package com.bkgy.modular.earlyWarningAnalysis.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.earlyWarningAnalysis.entity.CountNumEntity;
import com.bkgy.modular.earlyWarningAnalysis.entity.CountNumEntityVo;
import com.bkgy.modular.earlyWarningAnalysis.entity.StatisticalAnalysis;
import com.bkgy.modular.earlyWarningAnalysis.mapper.StatisticalAnalysisMapper;
import com.bkgy.modular.earlyWarningAnalysis.service.StatisticalAnalysisService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName StatisticalAnalysisServiceImpl
 * @date 2022/12/19 10:51
 */
@Service
public class StatisticalAnalysisServiceImpl extends ServiceImpl<StatisticalAnalysisMapper, StatisticalAnalysis> implements StatisticalAnalysisService {

    @Resource
    private StatisticalAnalysisMapper statisticalAnalysisMapper;
    /**
     * 预警统计分析
     * @param jsonObject
     * @return
     */
    @Override
    public ResultData findByStatistics(JSONObject jsonObject) {
        // 获取传入的参数信息
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 判断pageIndex和pageSize是否有值,没值给出提示
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        // 条件查询数据集合
        List<StatisticalAnalysis> analysisList = statisticalAnalysisMapper.findByData((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
        Integer count = statisticalAnalysisMapper.findByDataCount(condition);
        // 预警数量数据集合
        List<CountNumEntity> list = statisticalAnalysisMapper.findByCount(condition);
        List<CountNumEntity> countNumEntityList = new ArrayList<>();
        for (CountNumEntity item : list) {
            countNumEntityList.add(item);
        }
        String timeValue = condition.getString("timeValue");
        // 条件预警趋势变化集合
        List<CountNumEntity> timeCount = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        if (timeValue.equals("1")) {
            // 获取当前时间计算最近一日的数据
            // 获取当前日期
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 将当前时间往前推24小时
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,-1);
            // 开始时间
            String startTime = simpleDateFormat.format(calendar.getTime());
            for (int i = 0; i < 24; i++) {
                // startTime将开始时间每一个小时变一次
                // 将startTime转成Date对象
                Date parseStartTime = null;
                try {
                    parseStartTime = simpleDateFormat.parse(startTime);
                    Calendar instance = Calendar.getInstance();
                    instance.setTime(parseStartTime);
                    // 当前时间加一小时
                    instance.add(Calendar.HOUR,+1);
                    String parseStartTimePlus1 = simpleDateFormat.format(instance.getTime());

                    timeCount = statisticalAnalysisMapper.findTimeCount(startTime,parseStartTimePlus1);

                    for (CountNumEntity item : timeCount) {
                        countList.add(item.getCount());
                    }
                    startTime = parseStartTimePlus1;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            // 结束时间
            // String endTime = simpleDateFormat.format(date);
        } else if (timeValue.equals("2")) {
            // 获取当前时间计算最近一日的数据
            // 获取当前日期
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,-7);
            // 开始时间
            String startTime = simpleDateFormat.format(calendar.getTime());
            // 转成毫秒值进行计算
            try {
                long time = simpleDateFormat.parse(startTime).getTime()/1000;
                long time2 = time + 86400;
                String format = simpleDateFormat.format(time2 * 1000);
                // 结束时间
                String endTime = simpleDateFormat.format(date);
                // format不等于结束时间继续查询
                while (simpleDateFormat.parse(endTime).getTime() >= simpleDateFormat.parse(format).getTime()) {
                    timeCount = statisticalAnalysisMapper.findTimeCount(startTime,format);
                    for (CountNumEntity item : timeCount) {
                        countList.add(item.getCount());
                        startTime = format;
                        format = simpleDateFormat.format((simpleDateFormat.parse(format).getTime() / 1000 + 86400) * 1000);
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (timeValue.equals("3")) {
            // 获取当前时间计算最近一日的数据
            // 获取当前日期
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH,-1);
            // 开始时间
            String startTime = simpleDateFormat.format(calendar.getTime());
            // 转成毫秒值进行计算
            try {
                long time = simpleDateFormat.parse(startTime).getTime()/1000;
                long time2 = time + 86400;
                String format = simpleDateFormat.format(time2 * 1000);
                // 结束时间
                String endTime = simpleDateFormat.format(date);
                // format不等于结束时间继续查询
                while (simpleDateFormat.parse(endTime).getTime() >= simpleDateFormat.parse(format).getTime()){
                    timeCount = statisticalAnalysisMapper.findTimeCount(startTime,format);
                    for (CountNumEntity item : timeCount) {
                        countList.add(item.getCount());
                        startTime = format;
                        format = simpleDateFormat.format((simpleDateFormat.parse(format).getTime() / 1000 + 86400) * 1000);
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        CountNumEntityVo vo = new CountNumEntityVo();
        vo.setResultPageList(analysisList);
        vo.setResultPageCount(count);
        vo.setNumberOfAlertsList(list);
        vo.setNumberOfAlertsCount(list.size());
        vo.setResultTimeTrendCount(countList);
        return ResultData.ResultDataFaultSelf("操作成功",vo);
    }
}
